﻿using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using TridionDesktopTools.Core;
using System.Windows.Input;

namespace TridionDesktopTools.DocumentCreator
{
    public partial class MainWindow
    {
        public MainWindow()
        {
            InitializeComponent();

            this.cbBindingType.ItemsSource = Enum.GetNames(typeof(BindingType));
            this.cbBindingType.Text = Functions.GetFromIsolatedStorage("BindingType");
            if (String.IsNullOrEmpty(this.cbBindingType.Text))
                this.cbBindingType.SelectedIndex = 0;

            //get from isolated stoage
            this.txtHost.Text = Functions.GetFromIsolatedStorage("Host");
            this.txtUsername.Text = Functions.GetFromIsolatedStorage("Username");
            this.txtPassword.Password = Functions.GetFromIsolatedStorage("Password");

            this.txtFolder.Text = "C:\\";
        }

        private void btnConnect_Click(object sender, RoutedEventArgs e)
        {
            Functions.ResetClient();

            Functions.ClientBindingType = (BindingType)Enum.Parse(typeof(BindingType), this.cbBindingType.Text);
            Functions.SaveToIsolatedStorage("BindingType", this.cbBindingType.Text);

            if (!Functions.EnsureValidClient(this.txtHost.Text, this.txtUsername.Text, this.txtPassword.Password))
                return;

            this.lblPublications.Visibility = Visibility.Visible;
            this.cbPublications.Visibility = Visibility.Visible;

            this.cbPublications.ItemsSource = Functions.GetPublications(this.txtHost.Text, this.txtUsername.Text, this.txtPassword.Password);
            this.cbPublications.DisplayMemberPath = "Title";

            if (this.cbPublications.Items != null && this.cbPublications.Items.Count > 0)
            {
                this.cbPublications.SelectedIndex = 0;
                
                //save to isolated stoage
                Functions.SaveToIsolatedStorage("Host", this.txtHost.Text);
                Functions.SaveToIsolatedStorage("Username", this.txtUsername.Text);
                Functions.SaveToIsolatedStorage("Password", this.txtPassword.Password);
            }
        }

        private void cbPublications_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            ItemInfo item = this.cbPublications.SelectedValue as ItemInfo;
            if (item == null)
                return;

            this.lbSchema.ItemsSource = Functions.GetSchemas(this.txtHost.Text, this.txtUsername.Text, this.txtPassword.Password, item.TcmId).OrderBy(x => x.Title);
            this.lbSchema.DisplayMemberPath = "Title";

            this.lblSchema.Visibility = Visibility.Visible;
            this.lbSchema.Visibility = Visibility.Visible;
        }

        private void lbSchema_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            this.lblFolder.Visibility = Visibility.Visible;
            this.txtFolder.Visibility = Visibility.Visible;
            this.btnCreate.Visibility = Visibility.Visible;
        }

        private void txtFolder_OnMouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            var dialog = new System.Windows.Forms.FolderBrowserDialog();
            System.Windows.Forms.DialogResult result = dialog.ShowDialog();
            if (result == System.Windows.Forms.DialogResult.OK)
            {
                this.txtFolder.Text = dialog.SelectedPath;
            }
        }

        private void btnCreate_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                ItemInfo item = this.lbSchema.SelectedValue as ItemInfo;
                if (item == null || string.IsNullOrEmpty(item.TcmId))
                    return;

                Schema schemaData = Functions.GetSchemaData(this.txtHost.Text, this.txtUsername.Text, this.txtPassword.Password, item.TcmId);
                if (schemaData == null)
                {
                    throw new Exception("SchemaData is null.");
                }

                WordHelper wordHelper = new WordHelper();

                byte[] binWordFile = wordHelper.CreateWordDocument(schemaData, System.IO.Path.Combine(".", "schema.docx"));

                string responseFileName = System.IO.Path.Combine(this.txtFolder.Text, "Schema_" + item.Title.Replace(" ", "_") + ".docx");

                System.IO.File.WriteAllBytes(responseFileName, binWordFile);

                System.Diagnostics.Process.Start(responseFileName);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

    }
}
